<!--  -->
<template>
  <div>
    <div v-if="requestError" class="text-c errorPlace">
      <img width="100px" height="100px" :src="require('@/assets/images/pic_neterror.png')" alt="获取数据失败" />
      <div class="color-b font14" style="margin: 10px 0 0 0">
        获取数据失败!
      </div>
      <van-button plain round size="small" color="#7232dd" type="primary" @click="getdetail">点击重试</van-button>
    </div>
    <van-form v-else @submit="submitFromData">
      <van-tabs v-model="active" color="#1989fa" sticky animated line-width="33%" offset-top="50" @click="onClick">
        <van-tab title="基础信息">
          <tabone ref="toMsgs" :isdrawType='isdrawType' :canEdit='canEdit' :titledata='titledata'
            :businessdata='businessdata' :fromdata='fromdata' :showHistory='showHistory' :approveList='approveList'
            :historydata='historydata' :isStart='isStart' @freshData='freshData($event)' :pmInsType='pmInsType'/>
        </van-tab>
        <van-tab v-if="isdrawType==false" title="商机信息">
          <tabtwo :canEdit='canEdit' :fromdata='fromdata' :canEditFile='canEditFile' :huiqianType="huiqianType" :canHQEdit="canHQEdit" @freshData='freshData($event)' />
        </van-tab>
      </van-tabs>
      <div class="left_botton" v-if="showMsgBox">
        <button block type="button" @click="toMsgValue()" style="color:#60a9ec; display: inline-block;width:100%;border: 0px solid #60a9ec;border-radius: 15px 0px 0px 15px;
          padding:10px 10px 10px 15px;font-size:13px">意见</button>
      </div>
      <div v-if="todotype == true || toread == true || canEdit == true||isStart == true" class="footer">
        <van-button block type="info" native-type="submit" :disabled="canToNext == true ? false : true"
          style="display: inline-block;width:100%;border: 15px solid #60a9ec;">{{ toNextValue }}</van-button>
      </div>
    </van-form>

  </div>
</template>

<script>
import {getSrqrRequest, getWfLinkTracks, getWfOptMags, req} from '@/api/dictapi'
import util from "@/assets/js/public";
import tabone from "@/views/sjypdetail/tabone"
import tabtwo from "@/views/sjypdetail/tabtwo"
import { mapGetters } from "vuex"
import { Notify, Dialog, Toast } from 'vant'

export default {
  data() {
    return {
      active: 0,
      requestError: false,
      params: this.$route.query,
      showHistory: false,
      historydata: {},//已办显示当前办理人
      fromdata: {},//收入确认信息
      approveList: [],//审批意见列表
      titledata: {}, //主单据信息
      businessdata: {}, //商机基础信息
      showMsgBox: true,
      toNextValue: '流转下一步',
      canToNext: true,
      todotype: false,
      toread: false,
      isStart: false,
      canEdit: false,
      isdrawType: false,
      sjlistdata: {},
      pmInsType:'A',
      canEditFile:false,
      showHuishen:false,
      huiqianType:'',
      canHQEdit:false
    }
  },
  computed: {
    ...mapGetters(['user']),
    ...mapGetters(['userinfo'])
  },
  //生命周期--创建完成
  created() {

    if (this.params.todoType % 2 == 0) {
      this.showHistory = true;
    }
    if (this.params.isdraw == '1') {
      this.todotype = true;
      this.isStart = true
      this.isdrawType = true
      this.getdetail();
    } else if (this.params.todoType == '1') {
      this.todotype = true;
      this.toNextValue = '流转下一步'
      this.canToNext = true
      if (this.params.location == 'dict.start') {
        this.isStart = true
      } else if (this.params.location == 'dict.solutionManager') {
        this.canEdit = true
      } else if (this.params.location==="dict.countersignProvince"||this.params.location==="dict.countersignManager"
          ||this.params.location==="dict.countersignProvinceUp"||this.params.location==="dict.province"
          ||this.params.location==="dict.countersignManagerUp") { //会签阶段，判断当前参与会签人员应该填写哪个模块内容
        this.showHuishen = true
        this.canHQEdit = true
      }else if (this.params.location == 'dict.agree') { //会签完成，展示全部内容
        this.showHuishen = true
      }
      this.getdetail();
    } else if (this.params.todoType == '3') {
      this.toread = true;
      this.getdetail();
    } else if (this.params.todoType % 2 == 0) {
      this.getdetail();
    }
  },
  //生命周期--挂载完成
  components: {
    tabone,
    tabtwo,
  },
  activated() {
    if (localStorage.getItem('ztnllistdata') != '{}' && localStorage.getItem('ztnllistdata')) {
      console.log('放置了abilityInformationModeList')
      this.ztnllistdata = JSON.parse(localStorage.getItem('ztnllistdata'))
      if (!this.fromdata.abilityInformationModeList) {
        this.fromdata.abilityInformationModeList = []
      }
      this.fromdata.abilityInformationModeList.push(this.ztnllistdata)
      if (!this.fromdata.abilityInformationMapList) {
        this.fromdata.abilityInformationMapList = []
      }
      this.fromdata.abilityInformationMapList.push(this.ztnllistdata)
      this.fromdata = JSON.parse(JSON.stringify(this.fromdata));
      localStorage.setItem('ztnllistdata', '{}')
    }
  },
  //方法调用
  methods: {
    freshData(value) {
      console.log("父页面的数据刷新", this.canEdit)
      if (this.canEdit == true||this.isStart == true) {
        this.fromdata = value
      }
    },
    onClick(event) {
      console.log("切换tabs:", event)
      //对name进行赋值，此处name值取tabs中集合数据的位置顺序值
      if (event == 0 && this.isdrawType == false) {
        this.showMsgBox = true
      } else {
        this.showMsgBox = false
      }
    },
    toMsgValue() {
      this.$refs.toMsgs.tomsg()
    },
    goBack() {
      this.$router.back(-1) //返回上一页
    },
    onError(result) {
      this.requestError = true;
      Toast.clear();
      Notify(result.message);
    },
    getdetail() {
      Toast.loading({
        message: '加载中',
        forbidClick: false
      });
      if(this.isdrawType == true){
        getSrqrRequest("businessJudged$api$findByBusinessId",{
          kv: {
            "source": 'MOBILE',
            "businessId":this.params.businessId,
            "currentUserCode": util.encrypt(this.user.userName),
          },
          body:{
            "businessId":this.params.businessId,
          }
        }).then((result) => {
          Toast.clear();
          if (!result.data) {
            this.onError(result);
            return;
          }
          if (result.data.data) {
            this.fromdata = result.data.data
            this.titledata.title="商机研判起草"
          }
        })
      }else {
        getSrqrRequest("applicationFormProject$api$getFormDetail",{
          kv: {
            'source': 'MOBILE',
            'processInstId': this.params.processInstId,
            'workFlag': 'task',
            'location': this.params.location,
            'pmInsId': this.params.pmInsId,
            'pmInsType': this.params.pmInsType,
            "currentUserCode": util.encrypt(this.user.userName),
          }
        }).then((result) => {
          Toast.clear();
          if (!result.data) {
            this.onError(result);
            return;
          }
          if (result.data.data) {
            this.titledata = result.data.data
            this.getSrqrRequest()
          }
        })
      }
    },
    //获取两个number code
    getSrqrRequest() {
      Toast.loading({
        message: '加载中',
        forbidClick: false
      });
      getSrqrRequest("businessJudged$api$queryBusinessJudgedByPmInsId",{
        kv: {
          "source": 'MOBILE',
          "pmInsId":this.params.pmInsId,
          "currentUserCode": util.encrypt(this.user.userName),
        },
        body:{
          "pmInsId":this.params.pmInsId,
        }
      }).then((result) => {
        Toast.clear();
        if (!result.data) {
          this.onError(result);
          return;
        }
        if (result.data.data) {
          this.fromdata = result.data.data
          if(this.showHuishen==true){ //会签阶段
            console.log("会签阶段转换数据")
            this.fromdata.isImplementationEnd =this.fromdata.isImplementation
            this.fromdata.implementationTypeEnd  =this.fromdata.implementationType
            this.fromdata.implementScopeEnd  =this.fromdata.implementScope
            this.fromdata.isImplementationEnd   =this.fromdata.isImplementation
            this.fromdata.isProfessionalAbilityEnd   =this.fromdata.isProfessionalAbility
            this.fromdata.professionalAbilityExplainEnd  =this.fromdata.professionalAbilityExplain
            this.fromdata.abilityInformationStateEnd  =this.fromdata.abilityInformationState=='1'?'true':'false'
            this.fromdata.abilityInformationModeListEnd  =this.fromdata.abilityInformationModeList
            this.fromdata.abilityInformationExplainEnd =this.fromdata.abilityInformationExplain
            this.fromdata.isSecurityCapabilityEnd  =this.fromdata.isSecurityCapability
            this.fromdata.securityCapabilityEnd  =this.fromdata.securityCapability
            this.fromdata.securityCapabilityExplainEnd  =this.fromdata.securityCapabilityExplain
            this.fromdata.isFiveCapabilityEnd  =this.fromdata.isFiveCapability
            this.fromdata.fiveCapabilityEnd  =this.fromdata.fiveCapability
            this.fromdata.fiveCapabilityExplainEnd  =this.fromdata.fiveCapabilityExplain
          }
          this.fromdata.abilityInformationMapList  =this.fromdata.abilityInformationModeList
          if (this.showHistory == true) {
            this.getWfLinkTracks();
          } else {
            this.getWfOptMags()
          }
        }
      })
    },
    getWfLinkTracks() {
      getWfLinkTracks(Object.assign(this.params), { userName: this.user.userName })
        .then((result) => {
          Toast.clear();
          if (!result.data) {
            Notify(result.message);
            return;
          }
          if (result.data.data) {
            //数据过滤，并生成新的数组
            this.historydata = result.data.data.filter(item => item.CURRENTSTATE == 10)[0]
            if (this.historydata) {
              if (this.historydata.MANAGER) {
                this.showHistory = true;
              } else {
                this.showHistory = false;
              }
            } else {
              this.showHistory = false;
            }
          } else {
            this.showHistory = false;
          }
          this.getWfOptMags()
        })
    },

    getWfOptMags() {
      getWfOptMags(Object.assign(this.params), { userName: this.user.userName })
        .then((result) => {
          if (!result.data) {
            Notify(result.message);
            return;
          }
          this.approveList = result.data.data.reverse();
          if (this.params.todoType == '1'){
            this.getHuiqianQuanxian()
          }
        })
    },
    getHuiqianQuanxian(){
      let url=''
      if(this.params.location==="dict.countersignProvinceUp"||this.params.location==="dict.province"){// 省会签
        url = "businessJudged$api$findByUserNameProvince"
      }else{
        url = "businessJudged$api$findByUserNameCity"
      }
      req("dict",url,{
        kv : {currentUserCode: util.encrypt(this.user.userName),source: "MOBILE",userName: this.user.userName},
      }).then((result) => {
        Toast.clear();
        if (!result.data) {
          Notify(result.message);
          return;
        }
        this.huiqianType=''
        // let quanxianList=['融能管理员（9One)','融能管理员（IOT)','融能管理员（IDC)','融能管理员（移动云)','融能管理员（专线)'
        // ,'融能管理员（大数据)','融能管理员（5G)','融能管理员（物联网)','融能管理员（省专协同管理员)','融能管理员（自主实施管理员)'
        // ,'融能管理员（中台能力管理员)','融能管理员（安全能力管理员)','融能管理员（5G能力管理员)','市商机研判管理员','省商机研判管理员']
        if (result.data.data[0]=="融能管理员（9One)"){
          this.huiqianType='isNineOne'
        }else if (result.data.data[0]=="融能管理员（IOT)"){
          this.huiqianType='isIot'
        }else if (result.data.data[0]=="融能管理员（IDC)"){
          this.huiqianType='isIdc'
        }else if (result.data.data[0]=="融能管理员（移动云)"){
          this.huiqianType='isCloud'
        }else if (result.data.data[0]=="融能管理员（专线)"){
          this.huiqianType='isLine'
        }else if (result.data.data[0]=="融能管理员（大数据)"){
          this.huiqianType='isData'
        }else if (result.data.data[0]=="融能管理员（5G)"){
          this.huiqianType='isFive'
        }else if (result.data.data[0]=="融能管理员（物联网)"){
          this.huiqianType='isInternet'
        }else if (result.data.data[0]=="融能管理员（省专协同管理员)"){
          this.huiqianType='isProfessionalAbilityEnd'
        }else if (result.data.data[0]=="融能管理员（自主实施管理员)"){
          this.huiqianType='isImplementationEnd'
        }else if (result.data.data[0]=="融能管理员（中台能力管理员)"){
          this.huiqianType='abilityInformationStateEnd'
        }else if (result.data.data[0]=="融能管理员（安全能力管理员)"){
          this.huiqianType='isSecurityCapabilityEnd'
        }else if (result.data.data[0]=="融能管理员（5G能力管理员)"){
          this.huiqianType='isFiveCapabilityEnd'
        }else if (result.data.data[0]=="市商机研判管理员"){
          this.huiqianType='HYTime'
        }else if (result.data.data[0]=="省商机研判管理员"){
          this.huiqianType='HYTime'
        }
        console.log('当前的会签内容是'+this.huiqianType)
      })
    },
    // 流转下一步
    onNext() {
      if (this.huiqianType=='isNineOne'&&this.fromdata.isNineOne=='是'&&(!this.fromdata.energyFusionOneFile||this.fromdata.energyFusionOneFile.length<1)){
        Notify('请上传融能支撑方案文件');
        return;
      }else if (this.huiqianType=='isFive'&&this.fromdata.isFive=='是'&&(!this.fromdata.energyFusionFiveFile||this.fromdata.energyFusionFiveFile.length<1)){
        Notify('请上传融能支撑方案文件');
        return;
      }else if (this.huiqianType=='isIdc'&&this.fromdata.isIdc=='是'&&(!this.fromdata.energyFusionIdcFile||this.fromdata.energyFusionIdcFile.length<1)){
        Notify('请上传融能支撑方案文件');
        return;
      }else if (this.huiqianType=='isIot'&&this.fromdata.isIot=='是'&&(!this.fromdata.energyFusionIotFile||this.fromdata.energyFusionIotFile.length<1)){
        Notify('请上传融能支撑方案文件');
        return;
      }else if (this.huiqianType=='isLine'&&this.fromdata.isLine=='是'&&(!this.fromdata.energyFusionLineFile||this.fromdata.energyFusionLineFile.length<1)){
        Notify('请上传融能支撑方案文件');
        return;
      }else if (this.huiqianType=='isInternet'&&this.fromdata.isInternet=='是'&&(!this.fromdata.energyFusionInternetFile||this.fromdata.energyFusionInternetFile.length<1)){
        Notify('请上传融能支撑方案文件');
        return;
      }else if (this.huiqianType=='isCloud'&&this.fromdata.isCloud=='是'&&(!this.fromdata.energyFusionCloudFile||this.fromdata.energyFusionCloudFile.length<1)){
        Notify('请上传融能支撑方案文件');
        return;
      }else if (this.huiqianType=='isData'&&this.fromdata.isData=='是'&&(!this.fromdata.energyFusionBigDataFile||this.fromdata.energyFusionBigDataFile.length<1)){
        Notify('请上传融能支撑方案文件');
        return;
      }else if (this.huiqianType=='isImplementationEnd'&&this.fromdata.isImplementationEnd=='是'&&(!this.fromdata.isImplementationEndFile||this.fromdata.isImplementationEndFile.length<1)){
        Notify('请上传自主实施支撑方案文件');
        return;
      }else if (this.huiqianType=='isProfessionalAbilityEnd'&&this.fromdata.isProfessionalAbilityEnd=='是'&&(!this.fromdata.provinceSpecificFile||this.fromdata.provinceSpecificFile.length<1)){
        Notify('请上传省专协同方案文件');
        return;
      }else if (this.huiqianType=='abilityInformationStateEnd'&&this.fromdata.abilityInformationStateEnd=='true'&&(!this.fromdata.middleCapabilityFile||this.fromdata.middleCapabilityFile.length<1)){
        Notify('请上传使用中台能力方案文件');
        return;
      }else if (this.huiqianType=='isSecurityCapabilityEnd'&&this.fromdata.isSecurityCapabilityEnd=='是'&&(!this.fromdata.securityCapabilityFile||this.fromdata.securityCapabilityFile.length<1)){
        Notify('请上传使用安全能力方案文件');
        return;
      }else if (this.huiqianType=='isFiveCapabilityEnd'&&this.fromdata.isFiveCapabilityEnd=='是'&&(!this.fromdata.fiveCapabilityFile||this.fromdata.fiveCapabilityFile.length<1)){
        Notify('请上传使用5G能力方案文件');
        return;
      }else if (this.huiqianType=='HYTime'&&(!this.fromdata.meetingDate||!this.fromdata.meetingPlace)){
        Notify('请填写会议开始时间和会议地点');
        return;
      }
      // let otherList=this.fromdata.abilitySelect.split(',')
      // console.log("法分析后的数据时"+otherList)
      // for (let i in otherList){
      //   let names=otherList[i]
      //   this.fromdata[names] =true;
      // }
      console.log("会议地点是:", this.fromdata.meetingPlace)
      this.params.formData = this.fromdata;
      let formSendData = { "htmlParamMap": {} };
      formSendData.htmlParamMap = this.fromdata;
      formSendData.id = this.titledata.id;
      formSendData.pmInsId = this.titledata.pmInsId;
      formSendData.processType = this.params.pmInsType
      formSendData.workItemId = this.params.workItemId;
      formSendData.htmlParamMap.abilityInformationModeList=[]
      console.log("传给递交页面111111111111111:", formSendData.htmlParamMap.meetingPlace)
      var params1 = {
        kv: {
          currentUserCode: util.encrypt(this.user.userName),
          source: 'MOBILE',
          location: this.params.location,
          processInstId: this.params.processInstId,
          processDefName: this.params.processDefName,
          processType: this.params.pmInsType,
          isClaimFun: '3',
          isClaimCounty: '3',
          isOperateCity: '3',
          isOperateCounties: '3',
          isProvinceConfirm: '3',
          isCityConfirm: '3',
          isSupport:'是',
          isConverBusiness: this.fromdata.isConverBusiness,
          preAmount: this.fromdata.preAmount,
        },
        body: {
          location: this.params.location,
          processInstId: this.params.processInstId,
          processDefName: this.params.processDefName,
          source: 'MOBILE',
        }
      };

      var params2 = {
        kv: {
          "currentUserCode": util.encrypt(this.user.userName),
          "source": 'MOBILE',
          "location": this.params.location,
          "processInstId": this.params.processInstId,
          'processType': this.params.pmInsType,
          'receiptCode': this.params.processInstId
        },
      };
      if (this.fromdata.isSupport == true) {
        params2.kv.provinceSupport = this.fromdata.provinceSupport
      }
      var params3 = {
        kv: {
          "source": 'MOBILE',
          "currentUserCode": util.encrypt(this.user.userName),
          "workItemId": this.params.todoType == 3 ? "" : this.params.workItemId,
          "processInstId": this.params.processInstId,
          "location": this.params.location,
          "notificationId": this.params.todoType == 3 ? this.params.wfnid : ""
        },
      };
      localStorage.setItem('submitPageParam', JSON.stringify({
        getDecisionsUrl: 'decisionAndUser$api$getDecisions',
        getDecisionsParams: params1,
        getUserUrl: 'decisionAndUser$api$getOrgAndUser',
        getUserParams: params2,
        tosubmintUrl: 'applicationFormProject$api$startSubmitProcess',
        tosubmintParams: params3,
        formData: formSendData,
        fromMoa: this.params.fromMoa,
        type: this.params.todoType == 3 ? "toRead" : "toDo"
      }));
      this.$router.push(
        {
          name: 'srqrsubmit'
        }
      );
    },
    submitFromData() { //提交流转
      if (this.fromdata.informantPhone != null && this.fromdata.informantPhone != '') {
        const phonePass = this.newUtils.isPhoneNumber(this.fromdata.informantPhone)
        if (!phonePass) return;
      }
      console.log("传给递交页面:", JSON.stringify(this.fromdata))

      this.controlRule(this.params, this.fromdata.projectID, function () {
        this.onNext();
      }.bind(this));

    }
  },


}
</script>
<style scoped>
.errorPlace {
  position: absolute;
  margin-top: -80px;
  top: 50%;
  margin-left: -50px;
  left: 50%;
}
</style>